//
//  DXStatuesLabel.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/19.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit

class DXStatuesLabel: UILabel {

   var statuesLinks:[DXMatchRegModel]?
    var backViews:[UIView] = [UIView]()
    
    override var attributedText: NSAttributedString?{
    
        didSet{
       let muta = NSMutableAttributedString(attributedString: attributedText!)
           
            muta.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(kstatuesFontSize), range: NSMakeRange(0, muta.length))
            textView.attributedText = muta
            
    
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
 
        setUpUI()
       
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpUI(){
    userInteractionEnabled = true
        
      insertSubview(textView, atIndex: 0)

       
//        添加约束
        textView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
        }
        
    }
    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        for backVeiw in backViews{
        backVeiw.removeFromSuperview()
        }
        backViews.removeAll()
    }
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touchesCancelled(touches, withEvent: event)
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       let touch = touches.first
       let point = touch!.locationInView(self)
       let textRange = textView.characterRangeAtPoint(point)!
       textView.selectedTextRange = textRange
       let range = textView.selectedRange
      
        guard let links  = statuesLinks else{
        return
        }
        
        logPrint(links.count)
        for matchRegModel in links {
//            logPrint(matchRegModel.matchRange!)
            if NSLocationInRange(range.location, matchRegModel.matchRange!){
                logPrint("zai")
            textView.selectedRange = matchRegModel.matchRange!
            let textRange = textView.selectedTextRange
            let rects = textView.selectionRectsForRange(textRange!)
                for rect in rects{
                let selectFrame = (rect as! UITextSelectionRect).rect
                let backView = UIView(frame: selectFrame)
                backView.cornerRadius = 4
                backView.backgroundColor = RGBCorlor(R: 177, G: 215, B: 255)
                backView.frame = selectFrame
                insertSubview(backView, atIndex: 0)
                backViews.append(backView)
                }
            }else{
                logPrint("\(range)不在范围内\(matchRegModel.matchRange!)")
                
            }

        }
    }
    
//    MARK:懒加载
    lazy var textView:UITextView = {
    let textView = UITextView()
    textView.backgroundColor = UIColor.redColor()
        // 不允许编辑
        textView.editable = false
        // 不允许交互
        textView.userInteractionEnabled = false
        // 不允许滚动
        textView.scrollEnabled = false
        // 设置透明度为 0
        textView.alpha = 0.0
       
        textView.textContainerInset = UIEdgeInsetsMake(0, -5, 0, -5)
    return textView
    }()

    
}
